package com.mingqijia.gassafety.webserver.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mingqijia.gassafety.db.entity.AppealLog;
import com.mingqijia.gassafety.db.entity.HistoryAlarm;
import com.mingqijia.gassafety.db.entity.HistoryBroken;
import com.mingqijia.gassafety.db.mapper.AppealLogMapper;
import com.mingqijia.gassafety.shared.constant.AppealStateEnum;
import com.mingqijia.gassafety.webserver.service.AppealLogService;
import com.mingqijia.gassafety.webserver.service.HistoryAlarmService;
import com.mingqijia.gassafety.webserver.service.HistoryBrokenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 报警器诉求记录表 服务实现类
 * </p>
 *
 * @author guoliang.lao
 * @since 2023-05-17
 */
@Service
@Slf4j
public class AppealLogServiceImpl extends ServiceImpl<AppealLogMapper, AppealLog> implements AppealLogService {

    @Autowired
    private AppealLogMapper appealLogMapper;
    @Autowired
    HistoryAlarmService historyAlarmService;
    @Autowired
    HistoryBrokenService historyBrokenService;

    /**
     * 根据工单号查询工单处理详情
     *
     * @param orderNo
     * @return
     */
    @Override
    public AppealLog orderHistoryDetail(String orderNo) {
        AppealLog appealLog = appealLogMapper.selectByOrder(orderNo);
        log.info("orderHistoryDetail-appealLog：{}", JSONObject.toJSONString(appealLog));
        return appealLog;
    }

    @Override
    public void appealSparkRecord(String spId, Long id, Integer type) {
        log.info("appealSparkRecord-sparkOrder:{},id:{},type:{}",spId,id,type);
        try {
            AppealLog appealLog = new AppealLog();
            appealLog.setAppealType(type);
            appealLog.setAlarmBrokenId(id);
            appealLog.setAppealStatus(AppealStateEnum.HANDLED_TEST.getCode());
            appealLog.setUserName(AppealStateEnum.HANDLED_TEST.getMsg());
            appealLog.setUserAccount("");
            appealLog.setIsDeleted(false);
            appealLog.setSpId(spId);
            this.save(appealLog);
            // 更新报警/故障信息记录主表
            if (appealLog.getAppealType() == null || appealLog.getAppealType() == 0) {
                LambdaUpdateWrapper<HistoryAlarm> historyAlarmWrapper = new LambdaUpdateWrapper<>();
                historyAlarmWrapper.eq(HistoryAlarm::getId,appealLog.getAlarmBrokenId()).eq(HistoryAlarm::getIsDeleted,0);
                HistoryAlarm historyAlarm = new HistoryAlarm();
                historyAlarm.setWorkorderId(appealLog.getWorkorderId() == null ? 0L : appealLog.getWorkorderId());
                historyAlarm.setAppealStatus(appealLog.getAppealStatus());
                historyAlarm.setAppealResult(appealLog.getAppealResult());
                historyAlarm.setUserName(appealLog.getUserName());
                historyAlarm.setUserAccount(appealLog.getUserAccount());
                historyAlarm.setAppealTime(appealLog.getCreatedAt());
                boolean historyAlarmResult = historyAlarmService.update(historyAlarm,historyAlarmWrapper);
                log.info("工单处理完成，更新报警器设备报警记录,结果：{}，内容：{}",historyAlarmResult,historyAlarm.toString());
            } else if (appealLog.getAppealType() == 1) {
                LambdaUpdateWrapper<HistoryBroken> historyBrokenWrapper = new LambdaUpdateWrapper<>();
                historyBrokenWrapper.eq(HistoryBroken::getId,appealLog.getAlarmBrokenId()).eq(HistoryBroken::getIsDeleted,0);
                HistoryBroken historyBroken = new HistoryBroken();
                historyBroken.setWorkorderId(appealLog.getWorkorderId() == null ? 0L : appealLog.getWorkorderId());
                historyBroken.setAppealStatus(appealLog.getAppealStatus());
                historyBroken.setAppealResult(appealLog.getAppealResult());
                historyBroken.setUserName(appealLog.getUserName());
                historyBroken.setUserAccount(appealLog.getUserAccount());
                historyBroken.setAppealTime(appealLog.getCreatedAt());
                boolean historyBrokenResult = historyBrokenService.update(historyBroken,historyBrokenWrapper);
                log.info("工单处理完成，更新报警器设备故障记录,结果：{}，内容：{}",historyBrokenResult,historyBroken.toString());
            }
        } catch (Exception e) {
            log.error("appealSparkRecord-error:{}",e);
        }
    }
}
